#!/bin/bash
#---------------------------------------------#
#archlinux config
#---------------------------------------------#

# ##set The function of color_echo() to display the color
# function color_echo() {
#  	if [ $1 == "green" ]; then
#  		echo -e "\033[32;40m$2\033[0m"
#  	elif [ $1 == "red" ]; then
#  		echo -e "\033[31;40m$2\033[0m"
#  	fi
# }

if [ $USER != "root" ]; then
	echo  "please run The Shell with root or sudo."
fi



echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"
echo "+----------Installation and Configuration The Archlinux.-----------+"
echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"

#--------------------------------------------------------------#
#set The time of system#
echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"
echo "+----------------------Set The System Language.--------------------+"
echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"
sed -i "s/#en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/g" '/etc/locale.gen'
sed -i "s/#zh_CN.UTF-8 UTF-8/zh_CN.UTF-8 UTF-8/g" '/etc/locale.gen'
sed -i "s/#zh_TW.UTF-8 UTF-8/zh_TW.UTF-8 UTF-8/g" '/etc/locale.gen'

locale-gen
echo LANG=en_US.UTF-8 > /etc/locale.conf

echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"
echo "+--------------------------Set The Time Zone.----------------------+"
echo "+----- The select: 4 >>> 9 >>> 1 >>> 1 is Shanghai ----------------+"
echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"
tzselect

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

hwclock --systohc --utc
#Create the initial environment of The ramdisk#
mkinitcpio -p linux
#set The password of root#
echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"
echo "+--------------------Set The password of The root.-----------------+"
echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"
passwd 

#--------------------------------------------------------------#
#install of The bootloader BIOS/MBR#


echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"
echo "+--------------Install The grub and os-prober.---------------------+"
echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"

pacman -S grub os-prober
grub-install /dev/sda

grub-mkconfig -o /boot/grub/grub.cfg

#--------------------------------------------------------------#
#set The Network#
echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"
echo "+------------------------Set The Hostname.-------------------------+"
echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"
while true; do
	read -p "Please set The Hostname:" myhostname
	if [ -n "$myhostname" ]; then
		break;
	else
		echo "The Hostname can not be empty."
		continue;
	fi
done

echo $myhostname > /etc/hostname

cp /etc/hosts /etc/hosts_bak
sed -i  '6,7s/$/  '$myhostname' /g' /etc/hosts

#set The wired network.#
echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"
echo "+------------- Please Select The network connecting mode ----------+"
echo "+------------- Select 1 or 2 --------------------------------------+"
echo "+------------------------------------------------------------------+"
echo "+------------- wired Network: 1 -----------------------------------+"
echo "+------------- WIFI: 2 --------------------------------------------+"
echo "+------------------------------------------------------------------+"
echo "+------------------------------------------------------------------+"
read -p "Please Select the network connecting mode:" selectnet
if [ $selectnet == "1" ]; then
echo "+------------------------------------------------------------------+"
echo "+------------------------ The 1 is Wired Network ------------------+"
echo "+------------------------ Open The DHCP service . -----------------+"
echo "+------------------------------------------------------------------+"
	intface=$(ip link | grep BROADCAST | awk -F ": " '{ print $2 }')
	systemctl enable dhcpcd@$intface.service
elif [ $selectnet == "2" ]; then
echo "+------------------------------------------------------------------+"
echo "+------------------------- The 2 is WIFI --------------------------+"
echo "+-------------------- Install the WIFI firmware. ------------------+"
echo "+------------------------------------------------------------------+"
	pacman -S iw wpa_supplicant dialog
fi

#----------------------------------------------------------#
echo "+-----------------------------------------------------------------+"
echo "+------------------Set The password of The root.------------------+"
echo "+-----------------------------------------------------------------+"
passwd 

echo "+-----------------------------------------------------------------+"
echo "+-------------------Successful installation configuration---------+"
echo "+-----------------------------------------------------------------+"
echo "+------------Please Exit to the Chroot and Restart The System.----+"
echo "+-----------------------------------------------------------------+"
echo "+-------------------    -------------  -----  --------------------+"
echo "+----------------  -----  -----------  ---  ----------------------+"
echo "+---------------  ------  -----------  -  ------------------------+"
echo "+--------------  ------  ------------  --  -----------------------+"
echo "+---------------  ----  ------------  ----  ----------------------+"
echo "+----------------    ---------------  ------  --------------------+"
echo "+-----------------------------------------------------------------+"
echo "+-----------------------------------------------------------------+"
echo "+-----------------------------------------------------------------+"
echo "+-----------------------------------------------------------------+"
#-----------------------------------------------------------#
# # Install success, Quit to The Chroot#
# echo "#----------------------------------------------------------#"
# color_echo green "#------------------set success of The archlinux .---------------------#"
# while true; do
#         read -p "are you quit to the arch-chroot[Y/n]:" YN
#         if [ -n "$YN" ];then
#                 if [  $YN == "Y" -o   $YN == "y" -o $YN == "N" -o $YN == "n" ]; then
#                         break;
#                 else
#                 color_echo red "Please input the Y or n ."
#                 continue;
#                 fi
#         else
#                 color_echo red "Please input the Y or n ."
#                 continue;
#         fi
# done
# if [ $YN == "y" -o $YN == "Y" ]; then
# 	echo "Install success,Quit  The arch-Chroot ."
# 	exit  
# else
# 	echo "Install success,no to Quit The arch-Chroot . "
# fi




